package com.training.hatton.dropboxclient;

import java.io.File;

import android.app.Application;

import com.training.hatton.dropboxclient.http.HttpManager;
import com.training.hatton.dropboxclient.images.ImageLoader;
import com.training.hatton.dropboxclient.service.ServiceHelper;

public class DropboxApplication extends Application {

	private ServiceHelper serviceHelper;
	private ImageLoader imageLoader;
	private HttpManager httpManager;

	@Override
	public void onCreate() {
		super.onCreate();
		ContextHolder.getInstance().setContext(this);
		serviceHelper = new ServiceHelper(this);
		imageLoader = new ImageLoader(this);
		httpManager = new HttpManager(this);
	}
	
	@Override
	public Object getSystemService(String name) {
		if(name.equals(ImageLoader.IMAGE_LOADER)){
			return imageLoader;
		}else if(name.equals(ServiceHelper.SERVICE_HELPER)){
			return serviceHelper;
		}else if(name.equals(HttpManager.HTTP_MANAGER)){
			return httpManager;
		}
		return super.getSystemService(name);
	}
	
	@Override
	public void onLowMemory() {
		File[] fileList = getCacheDir().listFiles();
		for (File file : fileList) {
			file.delete();
		}
		super.onLowMemory();
	}
}